package com.wjb.xietong.component;

import java.util.Stack;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class ActManager {
	private static Stack<Activity> activityStack;
	private static ActManager instance = new ActManager();

	private ActManager() {
	}

	public static ActManager Instance() {
		return instance;
	}
	
	/**
	 * 跳转至下一页面
	 * @param packageContext
	 * @param cls
	 */
	public void forwardActivity(Context packageContext, Class<?> cls){
		Intent intent = new Intent(packageContext, cls);
		packageContext.startActivity(intent);
	}
	
	/**
	 * 跳转至下一页面， 传递 String参数
	 */
	public void forwardActivity(Context packageContext, Class<?> cls, String key, String extra){
		Intent intent = new Intent(packageContext, cls);
		intent.putExtra(key, extra);
		packageContext.startActivity(intent);
	}
	
	/**
	 * 跳转至下一页面， 传递 Long参数
	 */
	public void forwardActivity(Context packageContext, Class<?> cls, String key, long extra){
		Intent intent = new Intent(packageContext, cls);
		intent.putExtra(key, extra);
		packageContext.startActivity(intent);
	}
	
	/**
	 * 跳转至下一页面， 传递 int 参数
	 */
	public void forwardActivity(Context packageContext, Class<?> cls, String key, int extra){
		Intent intent = new Intent(packageContext, cls);
		intent.putExtra(key, extra);
		packageContext.startActivity(intent);
	}
	
	/**
	 * 将该 activity 入画面栈
	 * @param activity
	 */
	public void pushActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		if (activityStack.contains(activity)) {
			activityStack.remove(activity);
		}
		activityStack.add(activity);
	}
	
	/**
	 * 返回上一个 activity
	 */
	public void popActivity() {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		Activity activity = activityStack.lastElement();
		if (activity != null) {
			if(!activity.isFinishing())
				activity.finish();
			activity = null;
		}
	}

	/**
	 * 弹出画面栈中的某一个 activity
	 * @param activity 要弹出的 activity 对象
	 */
	public void popActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		if (activity != null) {
			activityStack.remove(activity);
			if(!activity.isFinishing())
				activity.finish();
			activity = null;
		}
	}

	/**
	 * 获取当前的 activity
	 * @return
	 */
	public Activity currentActivity() {
		if (activityStack == null || activityStack.size() == 0) {
			return null;
		}
		Activity activity = activityStack.lastElement();
		return activity;
	}

	/**
	 * 清空画面栈，退出程序
	 */
	public void popAllActivity() {
		while (true) {
			Activity activity = currentActivity();
			if (activity == null) {
				break;
			}
			popActivity(activity);
		}
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);
	}
	
	/**
	 * 弹出所有画面栈，仅保留某一个 activity
	 * @param cls 要保留的 activity 类
	 */
	public void popAllActivityExceptOne(Class<?> cls) {
		while (true) {
			Activity activity = currentActivity();
			if (activity == null) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			popActivity(activity);
		}
	}
	
	/**
	 * 判断画面栈中是否存在该 activity 对象
	 * @param activity
	 * @return 存在返回TRUE ，不存在返回FALSE
	 */
	public boolean existActivity(Activity activity){
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		if (activityStack.contains(activity)) {
			return true;
		}
		return false;
	}
}
